package com.wheat.vipcard.bean;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.wheat.vipcard.utils.PageResultSet;
import com.wheat.vipcard.utils.PageUtil;

/**
 * A data access object (DAO) providing persistence and search support for
 * Userinfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.wheat.vipcard.bean.Userinfo
 * @author MyEclipse Persistence Tools
 */

public class UserinfoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserinfoDAO.class);
	// property constants
	public static final String FIRST_NAME = "firstName";
	public static final String SEX = "sex";
	public static final String PHONE = "phone";

	public void save(Userinfo transientInstance) {
		log.debug("saving Userinfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Userinfo persistentInstance) {
		log.debug("deleting Userinfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Userinfo findById(java.lang.Integer id) {
		log.debug("getting Userinfo instance with id: " + id);
		try {
			Userinfo instance = (Userinfo) getSession().get("com.wheat.vipcard.bean.Userinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<?> findByExample(Userinfo instance) {
		log.debug("finding Userinfo instance by example");
		try {
			List<?> results = getSession().createCriteria("com.wheat.vipcard.bean.Userinfo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<?> findByProperty(String propertyName, Object value) {
		log.debug("finding Userinfo instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Userinfo as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<?> findByFirstName(Object firstName) {
		return findByProperty(FIRST_NAME, firstName);
	}

	public List<?> findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List<?> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List<?> findAll() {
		log.debug("finding all Userinfo instances");
		try {
			String queryString = "from Userinfo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Userinfo merge(Userinfo detachedInstance) {
		log.debug("merging Userinfo instance");
		try {
			Userinfo result = (Userinfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Userinfo instance) {
		log.debug("attaching dirty Userinfo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Userinfo instance) {
		log.debug("attaching clean Userinfo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param entity
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PageResultSet<Userinfo> findUserByPage(Userinfo entity, int pageNo, int pageSize) {
		PageResultSet<Userinfo> page = new PageResultSet<Userinfo>();
		try {
			String sqlString = buildSQuerySql(entity);
			int totalRow = queryCount(sqlString);
			PageUtil pages = new PageUtil(totalRow, pageSize, pageNo);
			List<Userinfo> list = new ArrayList<Userinfo>();
			List<Object[]> uList = queryList(sqlString, pageNo, pageSize);
			for (Object[] b : uList) {
				Userinfo u = new Userinfo();
				u.setId(b[0] == null ? 0 : Integer.parseInt(b[0].toString()));
				u.setFirstName(b[1] == null ? "" : b[1].toString());
				u.setSex(b[2] == null ? 0 : Integer.parseInt(b[2].toString()));
				u.setSexName(b[2] == "0" ? "男" : "女");
				u.setPhone(b[3] == null ? "" : b[3].toString());
				list.add(u);
			}
			page.setList(list);
			page.setPage(pages);
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error("System error!!!" + e);
			}
		}
		return page;
	}

	/**
	 * 构建查询SQL
	 * 
	 * @param entity
	 * @return
	 */
	public String buildSQuerySql(Userinfo entity) {
		String sqlStr = "select * from userInfo u where 1=1 ";
		if (entity != null) {
			if (StringUtils.isNotBlank(entity.getFirstName())) {
				sqlStr += " and u.firstName='" + entity.getFirstName() + "'";
			}
			if (entity.getSex() != -1) {
				sqlStr += " and u.sex='" + entity.getSex() + "'";
			}
		}
		return sqlStr;

	}
}